﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.SqlClient;
using System.Data;
namespace DAO
{
    public class DAO_Lop
    {
        public static List<DTO_Lop> SelectLop()
        {
            List<DTO_Lop> listLop = new List<DTO_Lop>();
            try
            {
                string s = "Proc_SelectDMLop";
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = ketnoi.Cmb();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = s;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataTable dt = new DataTable();
                da.Fill(dt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DTO_Lop lop = new DTO_Lop();
                    lop.Malop = dt.Rows[i]["Malop"].ToString();
                    lop.TenLop = dt.Rows[i]["TenLop"].ToString();
                    lop.SiSo = int.Parse(dt.Rows[i]["Siso"].ToString());
                    lop.MaKH = dt.Rows[i]["MaKH"].ToString();
                    lop.HeDT = dt.Rows[i]["HeDT"].ToString();
                    listLop.Add(lop);
                }
                ketnoi.Cmb().Close();
                return listLop;
            }
            catch (Exception e)
            {
                return listLop;
            }
        }
        public static List<DTO_Lop> SelectLop_theoKhoaHoc(string makhoahoc)
        {
            List<DTO_Lop> listLop = new List<DTO_Lop>();
            try
            {
                string s = "proc_selectLop_TheoKhoaHoc";
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = ketnoi.Cmb();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = s;
                cmd.Parameters.Add("@MaKH", SqlDbType.NVarChar).Value = makhoahoc;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataTable dt = new DataTable();
                da.Fill(dt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DTO_Lop lop = new DTO_Lop();
                    lop.Malop = dt.Rows[i]["Malop"].ToString();
                    lop.TenLop = dt.Rows[i]["TenLop"].ToString();
                    lop.SiSo = int.Parse(dt.Rows[i]["Siso"].ToString());
                    lop.MaKH = dt.Rows[i]["MaKH"].ToString();
                    lop.HeDT = dt.Rows[i]["HeDT"].ToString();
                    listLop.Add(lop);
                }
                ketnoi.Cmb().Close();
                return listLop;
            }
            catch (Exception e)
            {
                return listLop;
            }
        }
        public static List<DTO_Lop> SelectLop_theoKhoa(string makhoa)
        {
            List<DTO_Lop> listLop = new List<DTO_Lop>();
            try
            {
                string s = "proc_selectLop_TheoKhoa";
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = ketnoi.Cmb();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = s;
                cmd.Parameters.Add("@MaKhoa", SqlDbType.NVarChar).Value = makhoa;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataTable dt = new DataTable();
                da.Fill(dt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DTO_Lop lop = new DTO_Lop();
                    lop.Malop = dt.Rows[i]["Malop"].ToString();
                    lop.TenLop = dt.Rows[i]["TenLop"].ToString();
                    lop.SiSo = int.Parse(dt.Rows[i]["Siso"].ToString());
                    lop.MaKH = dt.Rows[i]["MaKH"].ToString();
                    lop.HeDT = dt.Rows[i]["HeDT"].ToString();
                    listLop.Add(lop);
                }
                ketnoi.Cmb().Close();
                return listLop;
            }
            catch (Exception e)
            {
                return listLop;
            }
        }
        public static bool deleteLop(string malop)
        {
            
            // lay danh sach lop theo ma lop
            List<DTO_SinhVien> listSV = new List<DTO_SinhVien>();
            try
            {
                string s = "Proc_SelectDMSinhVienTheoLop";
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = ketnoi.Cmb();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = s;
                cmd.Parameters.Add("@MaLop", SqlDbType.NVarChar).Value = malop;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataTable sv = new DataTable();
                da.Fill(sv);
                for (int i = 0; i < sv.Rows.Count; i++)
                {
                    DTO_SinhVien sinhvien = new DTO_SinhVien();
                    sinhvien.MaSV = sv.Rows[i]["MaSV"].ToString();
                    listSV.Add(sinhvien);
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            //end lay danh sach lop theo ma
            // xoa Tai Khoan va phan cong de tai
            for (int i = 0; i < listSV.Count; i++)
            {
                DAO_TaiKhoan.deleteTaiKhoanTheoMaSV(listSV[i].MaSV);
                DAO_PhanCongDT.deletePhanCongDTTheoMaSV(listSV[i].MaSV);
            }
             
            //xoa lop
            try
            {
                SqlConnection cn = ketnoi.Cmb();
                string s = "Proc_deleteLoptheoMaLop";
                SqlCommand cmd1 = new SqlCommand(s, cn);
                cmd1.CommandType = CommandType.StoredProcedure;
                cmd1.Parameters.AddWithValue("@Malop",malop);
                if (cn.State == ConnectionState.Closed)
                    cn.Open();
                cmd1.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
        public static bool insertLop(DTO_Lop lop)
        {
            try
            {
                SqlConnection cn = ketnoi.Cmb();
                string s = "Proc_InsertDMLop";
                SqlCommand cmd1 = new SqlCommand(s, cn);
                cmd1.CommandType = CommandType.StoredProcedure;
                cmd1.Parameters.AddWithValue("@Malop", lop.Malop);
                cmd1.Parameters.AddWithValue("@TenLop", lop.TenLop);
                cmd1.Parameters.AddWithValue("@Siso", lop.SiSo);
                cmd1.Parameters.AddWithValue("@MaKH", lop.MaKH);
                cmd1.Parameters.AddWithValue("@HeDT", lop.HeDT);
                if (cn.State == ConnectionState.Closed)
                    cn.Open();
                cmd1.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        public static bool updateLop(DTO_Lop lop,string malop_cu)
        {
            try
            {
                SqlConnection cn = ketnoi.Cmb();
                string s = "Proc_UpdateLop";
                SqlCommand cmd1 = new SqlCommand(s, cn);
                cmd1.CommandType = CommandType.StoredProcedure;
                cmd1.Parameters.AddWithValue("@Malop", lop.Malop);
                cmd1.Parameters.AddWithValue("@TenLop", lop.TenLop);
                cmd1.Parameters.AddWithValue("@Siso", lop.SiSo);
                cmd1.Parameters.AddWithValue("@MaKH", lop.MaKH);
                cmd1.Parameters.AddWithValue("@HeDT", lop.HeDT);
                cmd1.Parameters.AddWithValue("@Malop_Cu", malop_cu);
                if (cn.State == ConnectionState.Closed)
                    cn.Open();
                cmd1.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
        public static List<DTO_Lop> SelectKH_Theolop(string masv)
        {
            List<DTO_Lop> listLop = new List<DTO_Lop>();
            try
            {
                string s = "Proc_SelectKH_Theolop";
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = ketnoi.Cmb();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = s;
                cmd.Parameters.Add("@MaSV", SqlDbType.NVarChar).Value = masv;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataTable dt = new DataTable();
                da.Fill(dt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DTO_Lop lop = new DTO_Lop();
                    lop.Malop = dt.Rows[i]["Malop"].ToString();
                    lop.TenLop = dt.Rows[i]["TenLop"].ToString();
                    lop.SiSo = int.Parse(dt.Rows[i]["Siso"].ToString());
                    lop.MaKH = dt.Rows[i]["MaKH"].ToString();
                    lop.HeDT = dt.Rows[i]["HeDT"].ToString();
                    listLop.Add(lop);
                }
                ketnoi.Cmb().Close();
                return listLop;
            }
            catch (Exception e)
            {
                return listLop;
            }
        }
    }
}
